package piwan;

import java.io.File;

import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

public class ArrowRenderer {
	private GL gl;
	private static final String texFile = "gfx/arrow.png";
	private static final double texBottom = 84.0/256.0;
	private static final double texHeadStart = (256.0-60.0)/256.0;
	private double arrowWidth = 0.1, arrowY=0.1;
	private double headLength = texBottom/(1.0-texHeadStart)*arrowWidth;
	private Colour colour = new Colour(1.0,1.0,1.0,1.0);
	private Vector2 vfrom = new Vector2();
	private Vector2 vto = new Vector2();
	private boolean doUpdate = false;
	private boolean enabled = false;
	private Vector2 tailp1 = new Vector2();
	private Vector2 tailp2 = new Vector2();
	private Vector2 midp1 = new Vector2();
	private Vector2 midp2 = new Vector2();
	private Vector2	endp1 =new Vector2();
	private Vector2 endp2 = new Vector2();
	private Texture tex;
	
	public ArrowRenderer( MainWindow win ) {
		gl = win.getGL();
		loadTexture();
	}
	
	public void loadTexture() {
		try {
			tex = TextureIO.newTexture(new File(texFile), false);
		} catch( Exception e ) {
			MainWindow.die(e);
		}
	}
	
	public void setTo(Vector2 to) {
		this.vto.set(to);
		doUpdate = true;
	}
	
	public void setFrom(Vector2 from) {
		this.vfrom.set(from);
		doUpdate = true;
	}
	
	public void setEnabled(boolean b) { 
		enabled = b;
		doUpdate = true;
	}
	
	public void setColour(Colour col){
		colour.set(col);
	}
	
	
	public void render() {
		if(!enabled) return;
		
		if(doUpdate) {
			Vector2 dir = vto.minus(vfrom);
			double tailLength = dir.norm();
			if(tailLength==0.0) return;
			dir.mul(1.0/tailLength);
			tailLength -= headLength;
			
			Vector2 dtail = dir.times(tailLength);
			Vector2 perp = (new Vector2(-dir.y, dir.x)).times(arrowWidth/2.0);
			tailp1.set(vfrom).add(perp);
			tailp2.set(vfrom).sub(perp);
			midp1.set(vfrom).add(dtail);
			midp2.set(midp1).sub(perp);
			midp1.add(perp);
			endp1.set(vto).add(perp);
			endp2.set(vto).sub(perp);
		}
		gl.glEnable(GL.GL_TEXTURE_2D);
		gl.glDisable(GL.GL_LIGHTING);
		gl.glEnable(GL.GL_BLEND);
		tex.bind();
		gl.glColor4d(colour.r, colour.g, colour.b, colour.a);
		gl.glBegin(GL.GL_QUADS);
		// tail
		gl.glTexCoord2d(0.0, 0.0);
		gl.glVertex3d(tailp1.x,arrowY,tailp1.y);
		gl.glTexCoord2d(texHeadStart, 0.0);
		gl.glVertex3d(midp1.x,arrowY,midp1.y);
		gl.glTexCoord2d(texHeadStart, texBottom);
		gl.glVertex3d(midp2.x,arrowY,midp2.y);
		gl.glTexCoord2d(0.0, texBottom);
		gl.glVertex3d(tailp2.x,arrowY,tailp2.y);
		// head
		gl.glTexCoord2d(texHeadStart, 0.0);
		gl.glVertex3d(midp1.x,arrowY,midp1.y);
		gl.glTexCoord2d(1.0, 0.0);
		gl.glVertex3d(endp1.x,arrowY,endp1.y);
		gl.glTexCoord2d(1.0, texBottom);
		gl.glVertex3d(endp2.x,arrowY,endp2.y);
		gl.glTexCoord2d(texHeadStart, texBottom);
		gl.glVertex3d(midp2.x,arrowY,midp2.y);
		gl.glEnd();
	}
	
	//public void 
}
